﻿using System;
using System.Collections.Generic;
using System.Globalization;
using PublicTransportEnabler.net.Enum;
using System.Text;

namespace PublicTransportEnabler.net.Model
{
    public class Line : EquatableBase<Line>
    {
        private static long serialVersionUID = -5642533805998375070L;

        public string Id { get; private set; }
        private char Product { get; set; } // TODO make true field
        public string Label { get; private set; }
        public Style Style { get; private set; }
        public HashSet<Attr> Attrs { get; private set; }
        public string Message { get; private set; }

        private static string PRODUCT_ORDER = "IRSUTBPFC?";

        public Line(string id, string label, Style style)
            : this(id, label, style, null, null)
        {

        }

        public Line(string id, string label, Style style, string message)
            : this(id, label, style, null, message)
        {

        }

        public Line(string id, string label, Style style, HashSet<Attr> attrs)
            : this(id, label, style, attrs, null)
        {

        }

        public Line(string id, string label, Style style, HashSet<Attr> attrs, string message)
        {
            this.Id = id;
            this.Label = label;
            this.Style = style;
            this.Attrs = attrs;
            this.Message = message;

            this.Product = label != null ? label[0] : '?';
        }

        public bool HasAttr(Attr attr)
        {
            return Attrs != null && Attrs.Contains(attr);
        }
        
        public override string ToString()
        {
            StringBuilder builder = new StringBuilder("Line(");
            builder.Append(this.Label);
            builder.Append(")");
            return builder.ToString();
        }

        public override bool Equals(Line other)
        {
            return this.NullSafeEquals(this.Label, other.Label);
        }

        public override int InstanceGetHashCode()
        {
            return this.NullSafeHashCode(this.Label);
        }

        public override int CompareTo(Line other)
        {
            int productThis = PRODUCT_ORDER.IndexOf(this.Product);
            int productOther = PRODUCT_ORDER.IndexOf(other.Product);

            if (productThis < 0)
            {
                productThis = int.MaxValue;
            }

            if (productOther < 0)
            {
                productOther = int.MaxValue;
            }

            int compareProduct = productThis.CompareTo(productOther);

            if (compareProduct != 0)
                return compareProduct;

            return String.Compare(this.Label, other.Label, StringComparison.Ordinal);
        }
    }
}